package com.fenghuajueli.idiomppp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.fenghuajueli.idiomppp.R;
import com.fenghuajueli.idiomppp.entity.IdiomPinEntity;
import com.fenghuajueli.idiomppp.entity.IdiomShiYiEntity;

import java.util.List;

/**
 * 成语释义界面
 */
public class IdiomShiYiListAdapter extends BaseAdapter {

    private List<IdiomShiYiEntity> idiomPinEntityList;
    private Context mContext;
    private LayoutInflater mInflater;
    private int itemWidth;

    public IdiomShiYiListAdapter(Context context, List<IdiomShiYiEntity> idiomPinEntityList) {
        this.idiomPinEntityList = idiomPinEntityList;
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        itemWidth = (ScreenUtils.getScreenWidth() - ConvertUtils.dp2px(94)) / 10;
    }

    public void refreshData(List<IdiomShiYiEntity> idiomPinEntityList) {
        this.idiomPinEntityList = idiomPinEntityList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return idiomPinEntityList.size();
    }

    @Override
    public Object getItem(int i) {
        return idiomPinEntityList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.idiom_shiyi_item_layout, null);
            holder.idiomName = convertView.findViewById(R.id.idiomName);
            holder.tvIdiomShiYi = convertView.findViewById(R.id.tvIdiomShiYi);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        IdiomShiYiEntity idiomShiYiEntity = idiomPinEntityList.get(position);
        holder.idiomName.setText(idiomShiYiEntity.getIdiom() + "(" + idiomShiYiEntity.getPronounce() + ")");
        holder.tvIdiomShiYi.setText(idiomShiYiEntity.getExplain());
        return convertView;
    }

    final class ViewHolder {
        TextView idiomName;
        TextView tvIdiomShiYi;
    }

}
